//
//  SpeakViewController.m
//  geiTA
//
//  Created by duyan on 14-11-25.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "SpeakViewController.h"
#import "HomespeakTableViewCell.h"
#import <MessageUI/MessageUI.h>

@interface SpeakViewController ()

@end

@implementation SpeakViewController
- (void)dealloc
{
//    [_arr release];
    [super dealloc];
}
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
//        self.arr = [NSMutableArray array];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UITapGestureRecognizer *keyboardBack = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(keyboardBackAction)];
    [self.view addGestureRecognizer:keyboardBack];
    [keyboardBack release];
    _myWidth = [UIScreen mainScreen].bounds.size.width;
    _myHeight = [UIScreen mainScreen].bounds.size.height;
    self.title = @"意见反馈";
    
    self.navigationController.navigationBar.translucent = NO;
    [self creatLeftIterm];
    [self createBtn];
    [self createTextView];


    
    
//    [self creatTable];
}
//-(void)creatTable
//{
//    self.table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)style:UITableViewStylePlain];
//    _table.delegate = self;
//    _table.dataSource = self;
//    [self.table registerClass:[HomespeakTableViewCell class] forCellReuseIdentifier:@"ok"];
//    [self.view addSubview:_table];
//    [_table release];
//}
//-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
//{
//    return 1;
//}
//-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    HomespeakTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ok"];
//    return cell;
//}

//-(void)showMailPicker
//{
//    Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
//    if (mailClass !=nil) {
//        
//        if ([mailClass canSendMail]) {
//            
//            [self displayMailComposerSheet];
//            
//        }else{
//            
//            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@""message:@"设备不支持邮件功能" delegate:self cancelButtonTitle:@"确定"otherButtonTitles:nil];
//            
//            [alert show];
//            
//            [alert release];
//            
//        }
//        
//    }else{
//        
//        
//        
//    }
//    
//    
//    
//}
//
//-(void)displayMailComposerSheet
//
//{
//    
//    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
//    
//    
//    
//    picker.mailComposeDelegate =self;
// 
//    [picker setSubject:@"文件分享"];
//    
//    NSArray *toRecipients = [NSArray arrayWithObject:@"654722732@qq.com"];
//    
//    [picker setToRecipients:toRecipients];
//    NSString *address = @"www.baidu.com";
//    NSString *emailBody =[NSString stringWithFormat:@"我分享了文件给您，地址是%@", address] ;
//    [picker setMessageBody:emailBody isHTML:YES];
//    [picker release];
//}
//-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
//{
//    switch (result) {
//        case MFMailComposeResultCancelled:
//            
//            NSLog(@"Result: Mail sending canceled");
//            
//            break;
//            
//        case MFMailComposeResultSaved:
//            
//            NSLog(@"Result: Mail saved");
//            
//            break;
//            
//        case MFMailComposeResultSent:
//            
//            NSLog(@"Result: Mail sent");
//            
//            break;
//            
//        case MFMailComposeResultFailed:
//            
//            NSLog(@"Result: Mail sending failed");
//            
//            break;
//            
//        default:
//            
//            NSLog(@"Result: Mail not sent");
//            
//            break;
//            
//    }
//     
//    
//
//    
//}
-(void)creatLeftIterm
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    //    [button setBackgroundImage:[UIImage imageNamed:@"Personal_Arrow@2x.png"] forState:UIControlStateNormal];
    [button setTitle:@"return" forState:UIControlStateNormal];
    button.frame = CGRectMake(0, 0, 50, 20);
    [button addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc]initWithCustomView:button];
    
    self.navigationItem.leftBarButtonItem =leftBarButton;
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor blackColor];
    [leftBarButton release];
    
}

#pragma mark  leftBarButton 点击事件
-(void)backButtonAction:(id)sender
{
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    
}
- (void)createBtn
{
    self.okButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.okButton.frame = CGRectMake(_myWidth - 80,  20 ,  50 , 30 );
    self.okButton.layer.cornerRadius = 5;
    self.okButton.layer.masksToBounds = YES;
    [self.okButton setTitle:@"确定" forState:UIControlStateNormal];
    self.okButton.titleLabel.font = [UIFont systemFontOfSize:12.0];
    [self.okButton addTarget:self action:@selector(showPicher:) forControlEvents:UIControlEventTouchUpInside];
    self.okButton.backgroundColor = [UIColor colorWithRed:87 / 255.0 green:169 / 255.0 blue:221 / 255.0 alpha:1];
    self.but = [[UIBarButtonItem alloc] initWithCustomView:self.okButton];
    self.navigationItem.rightBarButtonItem = _but;
}
#pragma mark---创建textView---
- (void)createTextView;
{
    self.textView = [[UITextView alloc] initWithFrame:CGRectMake(_myWidth * 20 / 320,_myHeight * 10/ 480,_myWidth * 280 / 320 ,_myHeight * 230 / 480)];
    //    self.textView.alpha = 0.8;
    self.textView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:self.textView];
    [_textView release];
    
}
#pragma mark---回收键盘----
- (void)keyboardBackAction
{
    [_textView resignFirstResponder];
}


//1.前提导入库messageUI.framework
//2.写入头文件，<MessageUI/MFMailComposeViewController.h>
#pragma mark - 在应用内发送邮件
//激活邮件功能
- (void)showPicher:(UIButton *)button
{
    if (![self.textView.text isEqualToString:@""]) {
        //初始化MFMailComposeViewController，构造邮件体
        Class mailClass = NSClassFromString(@"MFMailComposeViewController");
        if (mailClass != nil) {
            //+ (BOOL)canSendMail方法检查用户是否设置了邮件账户
            if ([mailClass canSendMail]) {
                [self displayComposerSheet];//调出邮件发送窗口
            }else{
                [self launchMailAppOnDevice];//手机里的邮件信箱
            }
        }else{
            [self launchMailAppOnDevice];
        }
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Message Failed!" message:@"反馈信息不能为空" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles: nil];
        [alert show];
        [alert release];
    }
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        [self.navigationController popViewControllerAnimated:NO];
    }
}
//调出邮件发送窗口,可以发送邮件的话
-(void)displayComposerSheet
{
    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc]init];
    picker.mailComposeDelegate = self;
    //设置主题
    [picker setSubject:@"邮件主题"];
    
    
    //添加收件人
    NSArray *toRecipient = [NSArray arrayWithObject:@"654722732@qq.com"];
    
    [picker setToRecipients:toRecipient];
    
    
    // 添加图片
    UIImage *addPic = [UIImage imageNamed: @"1.jpg"];
    NSData *imageData = UIImagePNGRepresentation(addPic);            // png
    // NSData *imageData = UIImageJPEGRepresentation(addPic, 1);    // jpeg
    [picker addAttachmentData: imageData mimeType: @"" fileName: @"1.jpg"];
    
    NSString *emailBody = self.textView.text;//正文
    [picker setMessageBody:emailBody isHTML:NO];
    [self presentViewController:picker animated:YES completion:nil];
    [picker release];
}
#pragma mark - 实现 MFMailComposeViewControllerDelegate 处理邮件发送事件
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    //关闭邮件发送窗口
    if (result == MFMailComposeResultFailed) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Message Failed!" message:@"Your email has failed to send" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles: nil];
        [alert show];
        [alert release];
    }
    self.textView.hidden = YES;
    switch (result) {
        case MFMailComposeResultCancelled:
            _textView.text = @"Result: canceled邮件发送取消";
            [self launchMailAppOnDevice];
            break;
        case MFMailComposeResultSaved:
            _textView.text = @"Result: saved邮件保存成功";
            break;
        case MFMailComposeResultSent:
            _textView.text = @"Result: sent邮件发送成功";
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"用户反馈" message:@"发送成功" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
            
            [alert show];
            break;
        case MFMailComposeResultFailed:
            _textView.text = @"Result: failed邮件发送失败";
            break;
        default:
            _textView.text = @"Result: not sent";
            break;
    }
    [self dismissViewControllerAnimated:YES completion:nil];
    
}
-(void)launchMailAppOnDevice
{
    NSString *recipients = @"mailto:654722732@qq.com&subject=用户反馈!";
    //@"mailto:first@example.com?cc=second@example.com,third@example.com&subject=Hello from California!";
    NSString *body = [NSString stringWithFormat:@"&body=%@",self.textView.text];
    NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body];
    email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
}
-(void)viewDidUnload
{
    self.textView = nil;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
